<div class="row-content am-cf">
  <div class="row">
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
      <div class="widget am-cf">
        <div class="widget-head am-cf">
          <div class="widget-title am-cf">用户行为轨迹</div>
        </div>
        <div class="widget-body am-fr">

          <!-- 工具栏 -->
          <div class="page_toolbar am-margin-bottom-xs am-cf">
    <form class="toolbar-form am-form am-form-inline" action="">
        <input type="hidden" name="s" value="/<?= request()->pathinfo() ?>">

        <!-- 开始日期 -->
        <div class="am-form-group am-margin-right-xs">
            <input type="text" class="am-form-field" name="start_time"
                   placeholder="开始日期"
                   value="<?= $request->get('start_time') ?>"
                   data-am-datepicker readonly />
        </div>

        <!-- 结束日期 -->
        <div class="am-form-group am-margin-right-xs">
            <input type="text" class="am-form-field" name="end_time"
                   placeholder="结束日期"
                   value="<?= $request->get('end_time') ?>"
                   data-am-datepicker readonly />
        </div>

        <!-- 场景值下拉 -->
        <div class="am-form-group am-margin-right-xs">
            <?php $sceneSelected = $request->get('scene'); ?>
            <select name="scene"
                    data-am-selected="{btnSize:'sm',searchBox:1,maxHeight:280}">
                <option value="">场景值</option>
                <?php foreach ($sceneMap as $code => $text): ?>
                    <option value="<?= $code ?>" <?= (string)$sceneSelected === (string)$code ? 'selected' : '' ?>>
                        <?= $code ?> - <?= htmlentities($text) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>

        <!-- 查看全部按钮 -->
        <!-- <button type="button" class="am-btn am-btn-default am-btn-sm am-margin-right-xs"
                data-am-modal="{target: '#sceneModal'}">
            查看全部
        </button> -->

        <!-- 搜索框 -->
        <div class="am-input-group am-input-group-sm am-margin-right-xs" style="vertical-align: middle;margin-top: -9px;">
            <input type="text" class="am-form-field" name="search"
                   placeholder="行为/URL/昵称/姓名/电话/推荐人"
                   value="<?= $request->get('search') ?>">
            <span class="am-input-group-btn">
                <button class="am-btn am-btn-default am-icon-search" type="submit"></button>
            </span>
        </div>

        <!-- 导出按钮 -->
        <!-- <a id="btn-export" class="am-btn am-btn-success am-btn-sm" style="margin-top: -9px;">
            <i class="am-icon-download"></i> 导出
        </a> -->
    </form>
</div>

          <!-- 表格 -->
          <div class="am-scrollable-horizontal am-u-sm-12">
            <style>
              /* 列宽与省略号 */
              .col-url{ max-width:300px; }
              .text-ellipsis{
                display:inline-block; max-width:100%; overflow:hidden;
                text-overflow:ellipsis; white-space:nowrap; vertical-align:bottom;
              }
              /* 场景徽章更紧凑 */
              .scene-badge{ padding:2px 6px; font-size:12px; }
            </style>

            <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black am-text-nowrap">
              <thead>
                <tr>
                  <th>ID</th>
                  <th>姓名</th>
                  <th>电话</th>
                  <th>场景（中文）</th>
                  <th>场景值</th>
                  <th class="col-url">起始页面</th>
                  <th class="col-url">落地页面</th>
                  <th>分享者</th>
                  <th>记录时间</th>
                </tr>
              </thead>
              <tbody>
              <?php foreach ($list as $item): ?>
                <tr>
                  <td class="am-text-middle"><?= $item['id'] ?></td>
                  <td class="am-text-middle"><?= $item['patient_name'] ?: '未登录' ?></td>
                  <td class="am-text-middle"><?= $item['patient_phone'] ?: '—' ?></td>

                  <td class="am-text-middle"><?= htmlentities($item['scene_text']) ?></td>
                  <td class="am-text-middle">
                    <span class="am-badge am-badge-secondary am-radius scene-badge"
                          title="场景值"><?= (int)$item['action'] ?></span>
                  </td>

                  <!-- 起始/落地页面：省略号 + title 完整 -->
                  <td class="am-text-middle col-url" title="<?= htmlentities($item['fromroute'] ?? '') ?>">
                    <span class="text-ellipsis"><?= htmlentities($item['fromroute'] ?? '') ?></span>
                  </td>
                  <td class="am-text-middle col-url" title="<?= htmlentities($item['route'] ?? '') ?>">
                    <span class="text-ellipsis"><?= htmlentities($item['route'] ?? '') ?></span>
                  </td>

                  <td class="am-text-middle">
                    <?php
                      $refName = $item['referee_name'] ?: $item['referee_nick'] ?: '—';
                      echo htmlentities($refName);
                      if (!empty($item['referee_phone'])) {
                        echo '<small class="am-text-muted">(' . htmlentities($item['referee_phone']) . ')</small>';
                      }
                    ?>
                  </td>
                  <td class="am-text-middle"><?= $item['create_time'] ?></td>
                </tr>
              <?php endforeach; if ($list->isEmpty()): ?>
                <tr><td colspan="9" class="am-text-center">暂无记录</td></tr>
              <?php endif; ?>
              </tbody>
            </table>
          </div>

          <!-- 分页 -->
          <div class="am-u-lg-12 am-cf">
            <div class="am-fr"><?= $list->render() ?></div>
            <div class="am-fr pagination-total am-margin-right">
              <div class="am-vertical-align-middle">总记录：<?= $list->total() ?></div>
            </div>
          </div>

        </div>
      </div>
    </div>
  </div>
</div>

<!-- 场景值参照弹窗（默认收起） -->
<div class="am-modal am-modal-no-btn" tabindex="-1" id="sceneModal">
  <div class="am-modal-dialog" style="max-width:720px;">
    <div class="am-modal-hd">
      场景值清单
      <a href="javascript:void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
    </div>
    <div class="am-modal-bd" style="max-height:60vh; overflow:auto; text-align:left;">
      <ul class="am-list">
        <?php foreach ($sceneMap as $code => $text): ?>
          <li class="am-g">
            <div class="am-u-sm-3">
              <span class="am-badge am-badge-secondary am-radius scene-badge"><?= $code ?></span>
            </div>
            <div class="am-u-sm-9"><?= htmlentities($text) ?></div>
          </li>
        <?php endforeach; ?>
      </ul>
    </div>
  </div>
</div>

<script>
  // 导出：保持现有查询参数（含 scene / 时间 / 关键词），并把 s 改成导出路由
  $('#btn-export').on('click', function () {
    var params = $('.toolbar-form').serializeArray();
    var query = [];
    for (var i=0;i<params.length;i++){
      if (params[i].name === 's') continue; // 去掉当前页面 path
      query.push(encodeURIComponent(params[i].name) + '=' + encodeURIComponent(params[i].value));
    }
    var qs = query.length ? ('&' + query.join('&')) : '';
    // 注意：把下面 url() 里的路由改成你项目中的导出方法
    window.location = "<?= url('store/user/exportTrack') ?>" + qs;
  });
</script>